﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Xml.Linq;
using System.Reflection;
using ApiDoc;

namespace ApiMvcApplication.Controllers
{
    public class ApiDocumentationController : Controller
    {
        /// <summary>
        /// Returns the ApiDocumentationManager initialised and ready to use.
        /// </summary>
        /// <returns></returns>
        protected ApiDocumentationManager GetApiDocumentationManager(string language)
        {
            var manager = new ApiDocumentationManager();
            manager.LoadAssemblies(Server.MapPath("~/Content/AssembliesToDocument"));
            manager.LoadXmls(Server.MapPath("~/Content/AssembliesToDocument"));
            manager.CodeSyntaxLanguage = language;
            return manager;
        }

        public ActionResult Type(string id, string language = "C#")
        {
            // Work with URL restrictions.
            string cref = id.Replace('-', '.');
            var manager = GetApiDocumentationManager(language);
            var model = manager.GetTypeDocumentation(cref);
            return View(model);
        }

        public ActionResult Namespace(string id, string language = "C#")
        {
            // Work with URL restrictions.
            string cref = id.Replace('-', '.');
            var manager = GetApiDocumentationManager(language);
            var model = manager.GetNamespaceDocumentation(cref);
            return View(model);
        }

        public ActionResult Assembly(string id, string language = "C#")
        {
            // Work with URL restrictions.
            string cref = id.Replace('-', '.');
            var manager = GetApiDocumentationManager(language);
            var model = manager.GetAssemblyDocumentation(cref);
            return View(model);
        }

        public ActionResult Method(string id, string language = "C#")
        {
            // Work with URL restrictions.
            string cref = id.Replace('-', '.');
            var manager = GetApiDocumentationManager(language);
            var model = manager.GetMethodDocumentation(cref);
            return View(model);
        }

        public ActionResult Constructor(string id, string language = "C#")
        {
            // Work with URL restrictions.
            string cref = id.Replace('-', '.');
            var manager = GetApiDocumentationManager(language);
            var model = manager.GetConstructorDocumentation(cref);
            
            return View(model);
        }

        public ActionResult Event(string id, string language = "C#")
        {
            // Work with URL restrictions.
            string cref = id.Replace('-', '.');

            var manager = new ApiDocumentationManager();
            manager.AddAssemblyRange(AppDomain.CurrentDomain.GetAssemblies());
            manager.CodeSyntaxLanguage = language;

            var model = manager.GetEventDocumentation(cref);
            return View(model);
        }

        public ActionResult Field(string id, string language = "C#")
        {
            // Work with URL restrictions.
            string cref = id.Replace('-', '.');
            var manager = GetApiDocumentationManager(language);
            var model = manager.GetFieldDocumentation(cref);
            return View(model);
        }

        public ActionResult Property(string id, string language = "C#")
        {
            // Work with URL restrictions.
            string cref = id.Replace('-', '.');
            var manager = GetApiDocumentationManager(language);
            var model = manager.GetPropertyDocumentation(cref);
            return View(model);
        }
    }
}
